const express = require('express');
const Mock = require('mockjs');
const userRouter = express.Router();

/**
 * @api {GET} /api/users list
 * @apiDescription 获取用户列表
 * @apiName getUsers
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     [{
 *       "id": 1,
 *       "name": "张三"
 *     }]
 *
 * @apiGroup User
 * @apiVersion 1.0.0
 */
userRouter.get('/', (req, res) => {
  res.json(Mock.mock({
    'data|2': [{
      'id|+1': 1,
      name: '@cname',
    }],
  }).data)
});


/**
 * @api {POST} /api/users create
 * @apiName postUser
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "id": 1,
 *       "name": "张三"
 *     }
 * @apiGroup User
 * @apiVersion 1.0.0
 */
userRouter.post('/', (req, res) => {
  res.json(Mock.mock(
    {
      id: '@integer(1,10000)',
      name: '@cname',
    },
  ))
});


/**
 * @api {GET} /api/users/:id detail
 * @apiDescription 通过id获取用户
 * @apiParam (Path) {Number} id 用户id
 * @apiName getUserById
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "id": 1,
 *       "name": "张三"
 *     }
 * @apiGroup User
 * @apiVersion 1.0.0
 */
userRouter.get('/:id', (req, res) => {
  res.json(Mock.mock({
    id: parseInt(req.params.id),
    name: '@cname',
  }))
});

module.exports = userRouter;
